﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chefbook.DataObjects;
using System.Globalization;
using Chefbook.ViewModels;

namespace Chefbook.UserActions
{
    /// <summary>
    /// An action for deleting a category
    /// </summary>
    public class DeleteCategoryAction : UserActionBase
    {
        private CategoryViewModel categoryToDelete, oldParent;
        private List<RecipeViewModel> recipeList;

        /// <summary>
        /// Create a new action to delete a category
        /// </summary>
        /// <param name="categoryToDelete">The category to delete</param>
        public DeleteCategoryAction(CategoryViewModel categoryToDelete):base()
        {
            this.categoryToDelete = categoryToDelete;
            recipeList = new List<RecipeViewModel>(categoryToDelete.Recipes);
            oldParent = categoryToDelete.Parent;
        }

        /// <summary>
        /// Deletes the category
        /// </summary>
        public override void DoAction()
        {
            base.DoAction();
            categoryToDelete.CategoryModel.Delete();
        }

        /// <summary>
        /// Restores the category
        /// </summary>
        public override void UndoAction()
        {
            base.UndoAction();
            categoryToDelete.Parent = oldParent;
            foreach (var recipe in recipeList)
            {
                categoryToDelete.AddRecipe(recipe);
            }
        }

        /// <summary>
        /// Returns a user-friendly string describing the action
        /// </summary>
        /// <returns>A user-friendly string describing the action</returns>
        public override string ToString()
        {
            return string.Format(CultureInfo.CurrentUICulture, "Delete Category '{0}'", categoryToDelete);
        }
    }
}
